package com.jian.mall.ware.service.impl;

import com.jian.mall.common.utils.R;
import com.jian.mall.ware.entity.PurchaseDetailEntity;
import com.jian.mall.ware.feign.SkuInfo;
import com.jian.mall.ware.vo.StockVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.Query;

import com.jian.mall.ware.dao.WareSkuDao;
import com.jian.mall.ware.entity.WareSkuEntity;
import com.jian.mall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;
    @Autowired
    private SkuInfo skuInfo;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotEmpty(skuId) && !"0".equals(skuId)){
            wrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotEmpty(wareId) && !"0".equals(wareId)){
            wrapper.eq("ware_id",wareId);
        }


        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(PurchaseDetailEntity detail) {
        List<WareSkuEntity> list = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", detail.getSkuId())
                .eq("ware_id", detail.getWareId()));
        if (list == null || list.size() == 0){
            WareSkuEntity sku = new WareSkuEntity();
            sku.setSkuId(detail.getSkuId());
            sku.setStock(detail.getSkuNum());
            sku.setWareId(detail.getWareId());
            sku.setStockLocked(0);
            try {
                R info = skuInfo.info(detail.getSkuId());
                if (info.getCode() == 0){
                    Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                    sku.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){
            }
            wareSkuDao.insert(sku);
        }else {
            wareSkuDao.addStock(detail.getSkuId(),detail.getWareId(),detail.getSkuNum());
        }
    }

    @Override
    public List<WareSkuEntity> getBySkuId(Long skuId) {


        return baseMapper.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id",skuId));

    }

    @Override
    public List<StockVo> getBySkuId(List<Long> skuIds) {
        List<StockVo> stockVos = baseMapper.stockBySkuIds(skuIds);

        return stockVos;
    }
}